<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="108px">
      <el-form-item label="学校名称" prop="collegeName">
        <el-input
          v-model="queryParams.collegeName"
          placeholder="请输入学校名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     
      <el-form-item label="处理职工姓名" prop="collegeStaffName">
        <el-input
          v-model="queryParams.collegeStaffName"
          placeholder="请输入处理职工姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
       <el-form-item label="选择数据类型">
        <el-select v-model="value" @change="getList" placeholder="请选择">
          <el-option
            v-for="item in selectList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:record:add']"
        >新增</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:record:edit']"
        >查看详情</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:record:remove']"
        >批量处理</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:record:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="safetyHazardRecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="25" align="center" />
      <el-table-column label="标识" align="center" prop="id" />
      <el-table-column label="学校名称" align="center" prop="collegeName" />
      <el-table-column label="发起学生姓名" align="center" prop="studentName" />
      <el-table-column label="申请说明" align="center" prop="content" />
      <el-table-column label="发起时间" align="center" prop="createDt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
       <el-table-column label="是否已处理" align="center" prop="state" :formatter="statusFormat"/>
      <el-table-column label="处理职工姓名" align="center" prop="collegeStaffName" />
      <el-table-column label="处理说明" align="center" prop="dealContent" />
      <el-table-column label="处理时间" align="center" prop="dealDt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dealDt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
     
      <el-table-column label="操作" align="center" width="320" class-name="small-padding fixed-width">
        <template slot-scope="scope">
            <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:record:remove']"
          >立即处理</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:record:edit']"
          >查看详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改安全风险记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="学校名称" prop="collegeName">
          <el-input v-model="form.collegeName" placeholder="请输入学校名称" disabled="diasabledInput" />
        </el-form-item>
        <el-form-item label="发起学生姓名" prop="studentName">
          <el-input v-model="form.studentName" placeholder="请输入发起学生姓名" disabled="diasabledInput" />
        </el-form-item>
        <el-form-item label="图集" >
          <div class="demo-image__preview">
            <el-image 
              style="width: 100px; height: 100px"
              :src="url" 
              :preview-src-list="srcList">
            </el-image>
          </div>
        </el-form-item>
        <el-form-item label="申请说明" prop="content">
          <el-input v-model="form.content" type="textarea" placeholder="请输入内容"  disabled="diasabledInput"/>
        </el-form-item>
        <el-form-item label="发起时间" prop="createDt">
          <el-date-picker clearable size="small" style="width: 200px" disabled="diasabledInput"
            v-model="form.createDt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择发起时间">
          </el-date-picker>
        </el-form-item>
      
        <el-form-item label="处理职工姓名" prop="collegeStaffName">
          <el-input v-model="form.collegeStaffName" placeholder="请输入处理职工姓名" disabled="diasabledInput"/>
        </el-form-item>
        <el-form-item label="处理说明" prop="dealContent">
          <el-input v-model="form.dealContent" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="处理时间" prop="dealDt">
          <el-date-picker clearable size="small" style="width: 200px" disabled="diasabledInput"
            v-model="form.dealDt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择处理时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" width="320"  @click="submitForm">立即处理</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listsafetyHazardRecord, getsafetyHazardRecord, delsafetyHazardRecord, addsafetyHazardRecord, updatesafetyHazardRecord, exportsafetyHazardRecord,getSelectlist } from "@/api/system/safetyHazardRecord";

export default {
  name: "safetyHazardRecord",
  data() {
    return {
      url: '',
      srcList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 安全风险记录表格数据
      safetyHazardRecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        collegeName: null,
        studentName: null,
        content: null,
        dealContent: null,
        collegeStaffName: null,
        state:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        collegeId: [
          { required: true, message: "学校标识不能为空", trigger: "blur" }
        ],
        creatorId: [
          { required: true, message: "发起会员标识不能为空", trigger: "blur" }
        ],
        studentId: [
          { required: true, message: "发起学生标识不能为空", trigger: "blur" }
        ],
        handlerId: [
          { required: true, message: "处理人会员标识不能为空", trigger: "blur" }
        ],
        collegeStaffId: [
          { required: true, message: "处理人职工标识不能为空", trigger: "blur" }
        ],
      },
      selectList:[],
      value:'',
      // 状态字典
      statusOptions: [],
    };
  },
  created() {
    this.getList();
    this.getSelectlist();
      this.getDicts("stu_cl").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    getSelectlist(){
        getSelectlist().then(response => {
        this.selectList = response.data;
      });
    },
    /** 查询安全风险记录列表 */
    getList() {
      this.loading = true;
      if(this.value==''){
        this.queryParams.state=null;
      }else{
        this.queryParams.state=this.value;
      }
      listsafetyHazardRecord(this.queryParams).then(response => {
        this.safetyHazardRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.state);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        collegeId: null,
        collegeCode: null,
        collegeName: null,
        creatorId: null,
        studentId: null,
        studentName: null,
        location: null,
        photos: null,
        content: null,
        dealContent: null,
        handlerId: null,
        collegeStaffId: null,
        collegeStaffName: null,
        createDt: null,
        dealDt: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加安全风险记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getsafetyHazardRecord(id).then(response => {
        this.form = response.data;
        let arr = [];
        arr = this.form.photos.split(",");
        this.url = arr[0];
        this.srcList = arr;
        this.open = true;
        this.title = "查看安全风险记录详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
       if(this.form.state == 2){
         this.msgSuccess("已处理！");
         this.open = false;
        return;
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
            const ids = this.form.id;
            const dealContent = this.form.dealContent;
            this.$confirm('是否确认处理安全风险记录编号为"' + ids + '"的数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
              }).then(function() {
                return delsafetyHazardRecord(ids,dealContent);
              }).then(() => {
                this.getList();
                this.getSelectlist();
                this.msgSuccess("处理成功");
                this.open = false;
              }).catch(function() {});
              }
      });
    },
    /** 处理按钮操作 */
    handleDelete(row) {
      if(row.state == 2){
         this.msgSuccess("已处理！");
        return;
      }
      const ids = row.id || this.ids;
      this.$confirm('是否确认处理安全风险记录编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delsafetyHazardRecord(ids,'');
        }).then(() => {
          this.getList();
           this.getSelectlist();
          this.msgSuccess("处理成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有安全风险记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportsafetyHazardRecord(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
